
//
//  CourseHotCell.m
//  ShowYork
//
//  Created by hxh on 2020/8/28.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonHotCell.h"
#import "LessonPlanModel.h"
@interface LessonHotCell ()
@property (nonatomic,strong)UIImageView *coverImgView;
@property (nonatomic,strong)UIImageView *headerImgView;
@property (nonatomic,strong)UIImageView *iconImgView;
@property (nonatomic,strong)UILabel *nameLb;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)UILabel *contentLb;
@property (nonatomic,strong)UILabel *descLb;
@property (nonatomic,strong)CAGradientLayer *gradientLayer;
@property (nonatomic,strong)LessonPageItemModel *dtlModel;
@property (nonatomic,strong)UIButton *coverBtn;

@property (nonatomic,strong)UIView *line;

@end


@implementation LessonHotCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    self.coverImgView = [[UIImageView alloc]init];
    self.coverImgView.layer.cornerRadius = 5;
    self.coverImgView.layer.masksToBounds = YES;
    self.coverImgView.backgroundColor = RGBA(221, 221, 221, 1);
    [self.contentView addSubview:self.coverImgView];
    
    // 设置默认宽高比
    CGFloat rate = 70 / 125.0;
    
    [self.coverImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.contentView).offset(16);
        make.bottom.equalTo(self.contentView).offset(-16);
        make.width.mas_equalTo(KScaleWidth(125));
        make.height.mas_equalTo(KScaleWidth(125) * rate).priorityHigh();
    }];
    
    self.iconImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"course_tag_chinese"]];
    [self.contentView addSubview:self.iconImgView];
    
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.coverImgView).offset(3);
        make.left.equalTo(self.coverImgView.mas_right).offset(15);
    }];
    
    self.contentLb = [[UILabel alloc]init];
    self.contentLb.font = font(12);
    self.contentLb.textColor = C102;
    [self.contentView addSubview:self.contentLb];
    
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.iconImgView);
        make.right.equalTo(self.contentView).offset(-15);
    }];
    
    self.headerImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"defaut_header"]];
    self.headerImgView.layer.cornerRadius = 10;
    self.headerImgView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.headerImgView];
    [self.headerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.bottom.equalTo(self.coverImgView);
        make.left.equalTo(self.iconImgView);
    }];
    
    self.nameLb = [[UILabel alloc]init];
    self.nameLb.textColor = RGBA(153, 153, 153, 1);
    self.nameLb.font = font(12);
    [self.contentView addSubview:self.nameLb];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerImgView.mas_right).offset(8);
        make.bottom.equalTo(self.headerImgView);
    }];
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];
    
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(self.contentView);
    }];
    
    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"+加入学习" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.confirmBtn.backgroundColor = RGBA(204, 204, 204, 1);
    self.confirmBtn.titleLabel.font = font(12);
    self.confirmBtn.layer.cornerRadius = 13;
    self.confirmBtn.layer.masksToBounds = YES;
    [self.contentView addSubview:self.confirmBtn];
    
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 26));
        make.right.equalTo(self.contentView).offset(-15);
        make.bottom.equalTo(self.headerImgView);
    }];
    
    
    self.descLb = [[UILabel alloc]init];
    self.descLb.font = font(14);
    self.descLb.textColor = RGBA(51, 51, 51, 1);
    [self.contentView addSubview:self.descLb];
    [self.descLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImgView.mas_bottom).offset(2);
        make.left.equalTo(self.iconImgView);
        make.right.equalTo(self.contentView).offset(-15);
    }];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.confirmBtn layoutIfNeeded];
    
    self.gradientLayer =  [CAGradientLayer layer];
    self.gradientLayer.frame = self.confirmBtn.bounds;
    self.gradientLayer.endPoint = CGPointMake(0, 0);
    self.gradientLayer.startPoint  = CGPointMake(1.0, 0);
    self.gradientLayer.colors = @[(id)RGBA(119, 153, 255, 1).CGColor,(id)RGBA(86, 128, 250, 1).CGColor];
    
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"LessonHotCell";
    LessonHotCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[LessonHotCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (void)setCurrentModel:(LessonPageItemModel *)currentModel{
   
    self.dtlModel = currentModel;

    NSArray *ids = [RPAppManager getOwnerPlanIds];
    
    currentModel.isSeleted = [ids containsObject:currentModel.id];
    
 
    self.nameLb.text = currentModel.teacherName;
    [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:currentModel.head] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
    
    [self.coverImgView sd_setImageWithURL:[NSURL URLWithString:currentModel.cover]];
      
    if (currentModel.subResultId.intValue == 1) {
        self.iconImgView.image = [UIImage imageNamed:@"course_tag_chinese"];
    }
    if (currentModel.subResultId.intValue == 2) {
        self.iconImgView.image = [UIImage imageNamed:@"course_tag_math"];
    }
    if (currentModel.subResultId.intValue == 3) {
        self.iconImgView.image = [UIImage imageNamed:@"course_tag_english"];
    }
    self.confirmBtn.userInteractionEnabled = !currentModel.isSeleted;
    [self.confirmBtn setTitle:currentModel.isSeleted?@"已加入学习":@"+加入学习" forState:UIControlStateNormal];
    self.descLb.text = currentModel.title;
    self.confirmBtn.hidden = currentModel.showIntro;
    if (!currentModel.isSeleted) {
        if (![self.confirmBtn.layer.sublayers containsObject:self.gradientLayer]) {
            [self.confirmBtn.layer insertSublayer:self.gradientLayer atIndex:0];
        }
    }else{
        [self.gradientLayer removeFromSuperlayer];
    }
    NSMutableParagraphStyle*style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    //对齐方式
    style.alignment = NSTextAlignmentLeft;
    style.headIndent = 0.0f;//行首缩进进
    style.tailIndent = 0.0f;//行尾缩进
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    //首行缩进
    style.firstLineHeadIndent= self.iconImgView.image.size.width;
    NSAttributedString*attrText = [[NSAttributedString alloc] initWithString:currentModel.intro attributes:@{NSParagraphStyleAttributeName: style}];
    self.contentLb.attributedText = attrText;
}

- (void)isHideLine:(BOOL)isHide{
    self.line.hidden = isHide;
}

- (void)confirmMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(addorRemoveCourseHotPlanAtCurrentModel:)]) {
        [self.delegate addorRemoveCourseHotPlanAtCurrentModel:self.dtlModel];
    }
}

- (void)coverMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectedCaurseAtCurrentModel:)]) {
        [self.delegate selectedCaurseAtCurrentModel:self.dtlModel];
    }
}
@end
